Ext.define('Portal.controller.MainController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.main',
    listen : {
        global: {
            'toolbarClick':'checkToolbarType',
            'getNavStore':'getNavStore',
            'getCustomMenu':'getCustomMenu'
        },
        controller : {
            '#' : {
                unmatchedroute : 'onRouteChange'
            }
        },
        component:{
            "#mainContainerWrap":{
                beforeRender:'mainContainerWrapBeforeRender'
            }
        }
    },
    lastView: null,
    allMenuView:'',
    onNavigationTreeSelectionChange: function (tree, node) {
        var _this = this;
        if(node && node.length === 0) return;
        if(node && Ext.isArray(node)){node = node[0]}
        var to = node && node.get('routeId');
        if (to) this.redirectTo(to);
    },
    onToggleNavigationSize: function () {
        var me = this,
            refs = me.getReferences(),
            navigationList = refs.navigationTreeList,
            wrapContainer = refs.mainContainerWrap,
            collapsing = !navigationList.getMicro(),
            new_width = collapsing ? 64 : 200;
        if (Ext.isIE9m || !Ext.os.is.Desktop) {
            Ext.suspendLayouts();
            refs.webEasyLogo.setWidth(new_width);
            navigationList.setWidth(new_width);
            navigationList.setMicro(collapsing);
            Ext.resumeLayouts();
            wrapContainer.layout.animatePolicy = wrapContainer.layout.animate = null;
            wrapContainer.updateLayout();
        }
        else {
            if (!collapsing) {
                navigationList.setMicro(false);
            }
            refs.webEasyLogo.animate({dynamic: true, to: {width: new_width}});
            navigationList.width = new_width;
            wrapContainer.updateLayout({isRoot: true});
            navigationList.el.addCls('nav-tree-animating');
            if (collapsing) {
                navigationList.on({
                    afterlayoutanimation: function () {
                        navigationList.setMicro(true);
                        navigationList.el.removeCls('nav-tree-animating');
                    },
                    single: true
                });
            }
        }
    },
    onMainViewRender:function() {
        if (!window.location.hash) {
            this.redirectTo("home");
        }
        // this.allMenuView = Ext.create('Portal.view.toolbar.AllMenu');
    },
    onRouteChange:function(id){
        var navigationList = this.getReferences().navigationTreeList,
            store = navigationList.getStore();
        if(!store) return;
        this.checkNavigationType(id)
    },
    mainContainerWrapBeforeRender:function (view) {
        var _this = this;
        Ext.fireEvent('getApi','NavigationTree.json',function (data) {
            var root = JSON.parse(data[0].list).root;
            var store = Ext.create('Ext.data.TreeStore',{
                root:_this.getNavByUser(root)
            });
            var routeId = window.location.hash.replace(/#/,'');
            view.getComponent('navigationTreeList').setStore(store);
            _this.onRouteChange(routeId);
        });
    },
    getNavByUser:function (root) {
        // var userAccess = Cookies.get('roles') || ['sys_all','shimo','sys_access','pages_all','baidu','postMessage'];
        var userAccess = ['sys_all','shimo','sys_access','pages_all','baidu','postMessage'];
        root.children = this.info(root.children,userAccess);
        return root
    },
    info:function (root, access) {
        var _this = this;
        var nav = [];
        root.forEach(function (item, index) {
            if(access.indexOf(item.access)>=0){
                nav.push(item);
                if(item.children && item.children.length>0){
                    nav[index].children=_this.info(item.children,access)
                }
            }
        });
        return nav
    },
    checkNavigationType:function(id){
        var navigationList = this.getReferences().navigationTreeList,
            store = navigationList.getStore();
        var node = store.findNode('routeId', id);
        var mainCardPanel = this.checkTabExists(id);
        Boolean(node) === true ? navigationList.setSelection(node): navigationList.setSelection(false);
        if(!mainCardPanel) return;
        if(node){
            var viewType = node.data.viewType;
            if(viewType === 'url') this.addUrlTab(node,mainCardPanel);
            if(viewType === 'view') this.addViewTab(node,mainCardPanel);
            return
        }
        this.redirectTo(id);
    },
    checkToolbarType:function(item){
        var viewType = item.config.viewType;
        if(!viewType) return;
        var mainCardPanel = this.checkTabExists(item.config.href.replace(/#/,''));
        if(mainCardPanel){
            var node = {
                data:{
                    text:item.config.text,
                    routeId:item.config.href.replace(/#/,'')
                }
            };
            if(viewType === 'view'){
                node.data.viewId = item.config.viewId;
                this.addViewTab(node,mainCardPanel)
            }
            if(viewType === 'url'){
                node.data.viewUrl = item.config.viewUrl;
                this.addUrlTab(node,mainCardPanel)
            }
        }
    },
    addUrlTab:function (node,mainCardPanel) {
        if(mainCardPanel){
            mainCardPanel.add({
                title: node.data.text,
                itemId:node.data.routeId,
                frame:true,
                html:'<iframe id="'+node.data.routeId+'" src="'+node.data.viewUrl+'" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe>',
                closable: true
            });
            mainCardPanel.setActiveItem(node.data.routeId);
        }
    },
    addViewTab:function(node,mainCardPanel){
        if(mainCardPanel){
            mainCardPanel.add({
                title: node.data.text,
                itemId:node.data.routeId,
                items: Ext.create({
                    xtype: node.data.viewId,
                    routeId: node.data.routeId
                }),
                closable: true
            });
            mainCardPanel.setActiveItem(node.data.routeId);
        }
    },
    checkTabExists:function (routerId) {
        var mainCardPanel = this.getReferences().mainCardPanel;
        var tabExists = mainCardPanel.getComponent(routerId);
        if(tabExists){
            mainCardPanel.setActiveItem(routerId);
            return false;
        }
        return mainCardPanel
    },
    getNavStore:function (callback) {
        var navigationList = this.getReferences().navigationTreeList,
            store = navigationList.getStore();
        callback(store)
    },
    getCustomMenu:function (callback) {
        var customMenuCList = this.getReferences().customMenuContainer;
        var items = customMenuCList.menu.config.items;
        items.pop();items.pop();
        callback(items)
    },
    onAllMenuClick:function (view) {
        var _this = this;
        _this.allMenuView.hidden === true ?
            (function () {
                _this.allMenuView.show();
                view.addCls('toolbar-btn-active');
            })()
             :
            (function () {
                _this.allMenuView.close();
                view.removeCls('toolbar-btn-active');
            })()
    }
});
